<template>
  <j-modal :title="title" :width="width" :visible="visible" :confirmLoading="confirmLoading" switchFullscreen @ok="handleOk"
    @cancel="handleCancel" cancelText="关闭">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <a-form-item label="成品料号" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-popup v-decorator="['machinesortCode', validatorRules.machinesortCode]" :trigger-change="true" org-fields="id,materiel_code,product_name,gauge,unit,materiel_type"
            dest-fields="materialId,machinesortCode,materialName,materialGauge,unit,materialType" code="product_search" @callback="popupCallback" />
        </a-form-item>
        <a-form-item label="商品编码" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['productCode']" placeholder="请输入商品编码"></a-input>
        </a-form-item>
        <a-form-item label="成品名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['materialName']" placeholder="请输入物料名称"></a-input>
        </a-form-item>
        <a-form-item label="成品规格" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['materialGauge']" placeholder="请输入物料规格"></a-input>
        </a-form-item>
        <a-form-item label="类别" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['materialType']" placeholder="请输入类别"></a-input>
        </a-form-item>
        <a-form-item label="单位" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['unit']" placeholder="请输入单位"></a-input>
        </a-form-item>
        <!--<a-form-item label="生产阶别" :labelCol="labelCol" :wrapperCol="wrapperCol">-->
          <!--<j-list-select-tag type="list_multi" v-decorator="['query2',validatorRules.produceGrade]"-->
                             <!--:trigger-change="true" dictCode="query2" placeholder="请选择生产阶别" />-->
        <!--</a-form-item>-->
        <a-form-item label="生产阶别" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-multi-select-tag type="list_multi" v-decorator="['query2',validatorRules.produceGrade]"
                              :trigger-change="true" dictCode="produce_grade" placeholder="请选择生产阶别" />
        </a-form-item>
        <a-form-item label="BOM有效期" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <!-- <a-input v-decorator="['expiryDate']" placeholder="请输入BOM有效期"></a-input> -->
          <j-date placeholder="请选择BOM有效期" v-decorator="['expiryDate']" :trigger-change="true" style="width: 100%" />
        </a-form-item>
        <a-form-item label="版本" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['edition', validatorRules.edition]" placeholder="请输入版本"></a-input>
        </a-form-item>
        <!-- <a-form-item label="批次号" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['sectionNo']" placeholder="请输入批次号"></a-input>
        </a-form-item>
        <a-form-item label="公司编码" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['companyCode']" placeholder="请输入公司编码"></a-input>
        </a-form-item> -->
        <a-form-item label="Top文件" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-upload v-decorator="['topFiles']" :trigger-change="true"></j-upload>
        </a-form-item>
        <a-form-item label="BOTTOM文件" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-upload v-decorator="['bottomFiles']" :trigger-change="true"></j-upload>
        </a-form-item>
        <a-form-item label="坐标文件" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-upload v-decorator="['coordinateFiles']" :trigger-change="true"></j-upload>
        </a-form-item>
        <a-form-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-textarea v-decorator="['notes']" rows="4" placeholder="请输入备注" />
        </a-form-item>
        <a-form-item v-show="false" label="物料ID" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-show="false" v-decorator="['materialId']" placeholder="请输入物料ID"></a-input>
        </a-form-item>

      </a-form>
    </a-spin>
  </j-modal>
</template>

<script>
  import {
    httpAction
  } from '@/api/manage'
  import pick from 'lodash.pick'
  import {
    validateDuplicateValue
  } from '@/utils/util'
  import JDictSelectTag from "@/components/dict/JDictSelectTag"
  import JUpload from '@/components/jeecg/JUpload'
  import JDate from '@/components/jeecg/JDate'
  import JMultiSelectTag from '@comp/dict/JMultiSelectTag'

  export default {
    name: "MesChiefdataBomModal",
    components: {
      JDate,
      JDictSelectTag,
      JMultiSelectTag,
      JUpload,
    },
    data() {
      return {
        form: this.$form.createForm(this),
        title: "操作",
        width: 800,
        visible: false,
        model: {},
        labelCol: {
          xs: {
            span: 24
          },
          sm: {
            span: 5
          },
        },
        wrapperCol: {
          xs: {
            span: 24
          },
          sm: {
            span: 16
          },
        },

        confirmLoading: false,
        validatorRules: {
          machinesortCode: {
            rules: [{
              required: true,
              message: '请输入机种料号!'
            }, ]
          },
          edition: {
            rules: [{
              required: true,
              message: '请输入版本!'
            }, ]
          },
        },
        url: {
          add: "/chiefdata/mesChiefdataBom/add",
          edit: "/chiefdata/mesChiefdataBom/edit",
        }

      }
    },
    created() {},
    methods: {
      add() {
        this.edit({});
      },
      edit(record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model, 'machinesortCode', 'productCode', 'materialName',
            'materialGauge', 'materialType', 'expiryDate', 'sectionNo', 'companyCode', 'edition', 'topFiles',
            'bottomFiles', 'coordinateFiles', 'unit', 'notes', 'materialId'))
        })
      },
      close() {
        this.$emit('close');
        this.visible = false;
      },
      handleOk() {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if (!this.model.id) {
              httpurl += this.url.add;
              method = 'post';
            } else {
              httpurl += this.url.edit;
              method = 'put';
            }
            let formData = Object.assign(this.model, values);
            console.log("表单提交数据", formData)
            httpAction(httpurl, formData, method).then((res) => {
              if (res.success) {
                that.$message.success(res.message);
                that.$emit('ok');
              } else {
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }

        })
      },
      handleCancel() {
        this.close()
      },
      popupCallback(row) {
        console.log(11)
        this.form.setFieldsValue(pick(row, 'machinesortCode', 'productCode', 'materialName', 'materialGauge',
          'materialType', 'expiryDate', 'sectionNo', 'companyCode', 'edition', 'topFiles', 'bottomFiles',
          'coordinateFiles', 'notes', 'materialId','unit'))
      },


    }
  }
</script>
